<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;


    const AGENT = 2;
    const INVEST = 4;
    const PARTNER = 5;
    const USER = 0;

    public static $roles = [
        self::AGENT => '生产厂商',
        self::INVEST => '投资商',
        self::PARTNER => '合作厂商',
        self::USER => '个人',
    ];
    public static $blades = [
        self::AGENT => 'agent',
        self::INVEST => 'agent',
        self::PARTNER => 'agent',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'phone', 'password', 'name', 'gender', 'avatar'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'remember_token',
    ];


    protected $dates = ['deleted_at'];


    public function company()
    {
        return $this->hasOne(Company::class, 'id', 'company_id');
    }


    public function branches()
    {
        return $this->hasOne(Branches::class, 'id', 'branches_id');
    }

    //获取用户拥有的组
    public function group()
    {
        return $this->hasMany(Group::class, 'owner_id','id');
    }

    //获取用户所属的组
    public function groups()
    {
        return $this->belongsToMany(Group::class);
    }

    public function hasGroup(Group $group)
    {
        $groups = $this->group()->get();
        return $groups->contains($group);
    }

    public function belongsGroup($group)
    {
        $groups = $this->groups()->get();
        return $groups->contains($group);
    }

    public function getBlade()
    {
        $slug = array_get(self::$blades, $this->type);
        return 'company.partial.' . $slug;
    }

    public function isRole($role = 0)
    {
        return $role == $this->type;
    }

    public function getRole()
    {
        return array_get(self::$roles, $this->type);
    }

    public function isAgent()
    {
        return $this->isRole(self::AGENT);
    }
    public function isUser()
    {
        return $this->isRole(self::USER);
    }

    public function isInvest()
    {
        return $this->isRole(self::INVEST);
    }

    public function isPartner()
    {
        return $this->isRole(self::PARTNER);
    }

    //用户是否有押金
    public function isDeposit()
    {
        return $deposit = $this->deposit;
    }

    public function socialite()
    {
        return $this->hasMany(Socialite::class);
    }

    //获取用户订单
    public function order()
    {
        return $this->hasMany(Order::class, 'user_id', 'id');
    }

    //获取用户使用记录

    public function record()
    {
        return $this->hasMany(useRecord::class, 'user_id', 'id');
    }

    //获取退押金记录表
    public function refund()
    {
        return $this->hasMany(ReFund::class, 'user_id', 'id');
    }

    //获取投资方的所属柜子信息
    public function invChest()
    {
        return $this->hasMany(InvestorChest::class, 'user_id', 'id');
    }

    public function getSlug($type = '')
    {

        $slug = $this->slug ? $this->slug : '0_0_0';
        $data = explode('_', $slug);
        if ($type) {
            return $data;
        }
        $typeArr = [
            'group' => 0,
            'chest' => 1,
            'grip' => 2
        ];
        $flag = array_get($typeArr, $type);
        return array_get($data, $flag);

    }


    public static function getAgentSelectOptions()
    {
        //生产厂家
        $options = self::where(['type' => '3'])->pluck('name', 'id');
        return $options;
    }

    public static function getInvSelectOptions()
    {
        //投资方
        $options = self::where(['type' => '5'])->pluck('name', 'id');
        return $options;
    }

    public static function getParSelectOptions()
    {
        //合作厂家
        $options = self::where(['type' => '4'])->pluck('name', 'id');
        return $options;
    }

    //查询收入表
    public function income()
    {
        return $this->hasMany(Income::class, 'admin_user_id', 'id');
    }

    //查询可提现表
    public function fee()
    {
        return $this->hasOne(SumIncome::class, 'user_id', 'id');
    }

    //查询柜子
    public function chest()
    {
        return $this->hasMany(Chest::class, 'agent_id', 'id');
    }

    //查询提现记录
    public function drawal()
    {
        return $this->hasMany(WithDraWal::class, 'user_id', 'id');
    }


    public function canAdded()
    {
        return $this->group()->get() > '0';
    }

    public function canAddMember(Group $group, User $user = null)
    {


        //如果要添加用户不存在 或者无法被添加  那么返回失败
        if (!$user || !$user->canAdded()) {
            return false;
        }

        // 如果当前组属于当前用户 那么可以添加
        if ($this->hasGroup($group)) {
            return true;
        }

        // 如果用户组属于公司用户组  并且当前成员属于用户组成员  那么可以添加
        if ($group->isCompany()) {
            return $this->belongsGroup($group);
        }


    }


}
